<?php

namespace App\Http\Controllers;

use App\DetailNum;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class SignController extends Controller
{
    public function register(){
        return view('register');
    }
    public function registerdo(Request $request){
        $data['username'] = $request['username'];
        $data['password'] = $request['password'];
        $res = User::addOne($data);
        if($res){
            echo "<script>alert('注册成功')</script>";
            $where = User::getOne($data)->toArray()[0];
            $id = $where['id'];
            $numdata['user_id'] = $id;
            $numdata['addnum'] = 100;
            $numdata['source'] = "注册";
            $numdata['updated_at'] = date('y:m:d h:i:s',time());
            $numres = DetailNum::addOne($numdata);
            echo "<script>alert('注册赠送积分 100！')</script>";
            header("refresh:0;url=login");
        }
    }
    public function login(){
        return view('login');
    }
    public function logindo(Request $request){
        $data['username'] = $request['username'];
        $data['password'] = $request['password'];
        $res = User::login($data);
        if($res){
            $request->session()->put('username', $data['username']);
            echo "<script>alert('登录成功')</script>";
            $where = User::getOne($data)->toArray()[0];
            $id = $where['id'];
            $numdata['user_id'] = $id;
            $numdata['addnum'] = 10;
            $numdata['source'] = "登录";
            $numdata['updated_at'] = date('y:m:d h:i:s',time());
            $numres = DetailNum::addOne($numdata);
            echo "<script>alert('登录赠送积分 10！')</script>";
            header("refresh:0;url=show");

        }else{
            echo "<script>alert('登录失败')</script>";
            header("refresh:0;url=login");
        }
    }
    public function show(Request $request){

        $data['username'] = $request->session()->get('username');
        $where = User::getOne($data)->toArray()[0];
        $id['user_id'] = $where['id'];

        $redis = Redis::connection();
        if($redis->exists('data')){
            $numdata = $redis->get('data');
            $numdata = json_decode($numdata,true);
        }else{
            $numdata = DetailNum::getAll($id)->toArray();
            $redis->set('data',json_encode($numdata));
        }
        $sum = 0;
        foreach($numdata as $value){
            $sum += $value['addnum'];
            $sum -= $value['subnum'];
        }

        return view('show',['user'=>$data['username'],'sum'=>$sum,'data'=>$numdata]);
    }
    public function quit(Request $request){
        $data['username'] = $request->session()->get('username');
        $where = User::getOne($data)->toArray()[0];
        $id = $where['id'];
        $numdata['user_id'] = $id;
        $numdata['subnum'] = 5;
        $numdata['source'] = "退出登录";
        $numdata['updated_at'] = date('y:m:d h:i:s',time());
        $numres = DetailNum::addOne($numdata);

        $request->session()->forget('username');
        echo "<script>alert('退出登录扣除积分 5！')</script>";
        header("refresh:0;url=login");
    }
    public function sign(Request $request){
        $data['username'] = $request['username'];
        $where = User::getOne($data)->toArray()[0];
        $id = $where['id'];
        $numdata['user_id'] = $id;
        $numdata['addnum'] = 5;
        $numdata['source'] = "签到";
        $numdata['updated_at'] = date('y:m:d h:i:s',time());
        $numres = DetailNum::addOne($numdata);
        if($numres){
            return ['data'=>$data,'code'=>0,'msg'=>"签到成功"];
        }else{
            return ['data'=>"",'code'=>1,'msg'=>"签到失败"];
        }
    }
    public function info(Request $request){
        $data['username'] = $request->session()->get('username');
        $where = User::getOne($data)->toArray()[0];
        return view('info',['data'=>$where]);
    }
    public function infodo(Request $request){
        $where['id'] = $request['id'];
        $data['username'] = $request['username'];
        $data['password'] = $request['password'];
        $data['pic']      =  $request->file('file')->store('images');
        $data['tel']      =  $request['tel'];
        $res = User::upDo($where,$data);
        if($res){
            $id = $where['id'];
            $numdata['user_id'] = $id;
            $numdata['addnum'] = 20;
            $numdata['source'] = "完善信息";
            $numdata['updated_at'] = date('y:m:d h:i:s',time());
            $numres = DetailNum::addOne($numdata);
            echo "<script>alert('完善信息获得积分 20！')</script>";
            header("refresh:0;url=show");
        }else{
            echo "<script>alert('完善信息失败')</script>";
            header("refresh:0;url=show");
        }
    }
}
